package com.zhkj.shixi.tvplayer;

import android.app.Activity;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.SurfaceHolder;

import com.avery.subtitle.widget.SimpleSubtitleView;
import com.zhkj.shixi.tvplayer.Interface.IMediaPlayer;

import java.io.File;

/**
 * Created by suqiuyu on 19-8-8.
 */

public class MyMediaPlayer extends MediaPlayer implements IMediaPlayer{
    private Activity mActivity;
    public SimpleSubtitleView getmSubtitleView() {
        return mSubtitleView;
    }

    public void setmSubtitleView(SimpleSubtitleView mSubtitleView) {
        this.mSubtitleView = mSubtitleView;
    }

    private com.avery.subtitle.widget.SimpleSubtitleView mSubtitleView;
    private String path="";
    public MyMediaPlayer(Activity activity) {
        super();
        mActivity =activity;
        this.setOnErrorListener(new MediaPlayerError(mActivity));
    }
    @Override
    public void init(String url) {
        this.reset();
        try {
            this.setDataSource(url);
            this.prepare();
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.path=url;
    }
    @Override
    public void init(String url, SurfaceHolder holder) {
        this.reset();
        try {
            this.setDataSource(url);
            this.setOnPreparedListener(new OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mSubtitleView.bindToMediaPlayer(mp);
                }
            });
            this.prepare();
            this.setDisplay(holder);
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.path=url;
    }

    @Override
    public void play() {
        if(new File(path.substring(0,path.indexOf("."))+".srt").exists()) {
            mSubtitleView.setSubtitlePath(path.substring(0, path.indexOf(".")) + ".srt");
        }
        Log.w("aaaa",path.substring(0,path.indexOf("."))+".srt");
        if (!this.isPlaying()) {
            this.start();
        }
    }

    @Override
    public void pause() {
        if (this.isPlaying()) {
            super.pause();
        }
    }

    @Override
    public void setTime(int time) {
        this.seekTo(time);
    }

    @Override
    public void errorManage(String errornum) {

    }


}
